<html>
<head>
<script>


var req;
var settings =	{
					"displayAnotherPageInstead": "0",
					"pageToDisplayURL" : "",
					"hideMostVisited": "false",
					"hideSearches" : "false",
					"hideRecentBookmarks" : "false",
					"hideRecentlyClosedTabs" : "false"
				};

var myport;
				
chrome.self.onConnect.addListener(handleConnect);

function handleConnect(port) {

	console.log("Handling connect...");
	myport = port;
	myport.onMessage.addListener(handleMessage);
	console.log("Done handling connect...");
}

function handleMessage(message) {
	console.log("Handling message...");
	
	if(message != "getsettings"){
		console.error("Not getsettings");
		return;
	}
		
	req = new XMLHttpRequest();
	req.onload = loadconfig;
	
	console.log("Getting settings...");
	//TODO: Try/Catch
	req.open("GET",
			 "config.xml",
			 false);
	req.send(null);
	console.log("Done handling message...");
}

function loadconfig(){
	if( this.readyState == 4 ) {
		console.log("Loading config...");
		var xmlDoc = req.responseXML;
		bindSettings(xmlDoc);
		console.log("Done loading config...");
		console.log("Sending settings...");
		console.log(settings);
		myport.postMessage(settings);
		console.log("Done sending settings...");
	}
}

function bindSettings(xmlDoc){ 
	
	console.log("Binding settings... ");
	
	if(!xmlDoc || !xmlDoc.childNodes){
		console.error("No child nodes in XML file");
		console.error(xmlDoc);
		return;
	}
	
	console.log(xmlDoc);
	
	var child = xmlDoc.firstChild.firstElementChild;
	while(child != null){
		
		var tagName = child.tagName;
		var value = child.firstChild.data;
	
		console.log("Binding: " + tagName + " : " + value);
		settings[tagName] = value;
		
		child = child.nextElementSibling;
	}
	
	console.log("Done binding settings...");
}

</script>
</head>

<!-- body>
<div id="button"  onclick="window.open('dashboard.html')" class="toolstrip-button">
  <img id="icon" src="ui/icon.png" style="width:auto; height:auto"/>
</div>
</body -->

</html>